#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_CONTACTS 100
#define MAX_NAME 50
#define MAX_JOB 50
#define MAX_TITLE 50
#define MAX_PHONE 20
#define MAX_EMAIL 50
 
typedef struct{
	char name[MAX_NAME];
	char job[MAX_JOB];
	char title[MAX_TITLE];
	char phone[MAX_PHONE];
	char email[MAX_EMAIL];
}Contact;




typedef struct{
	Contact contacts[MAX_CONTACTS];
	int contact_Count;
}ContactList;



void addContact(ContactList *list){
	if (list->contact_Count >= MAX_CONTACTS){
		printf("通讯录已满，无法添加更多联系人。\n");
		return;
	}
	Contact new_Contact;
	printf("请输入姓名：\n");
	
	scanf(" %s", new_Contact.name);
	printf("请输入职务：\n");
	
	scanf(" %s", new_Contact.job);
	printf("请输入职称：\n");
	
	scanf(" %s", new_Contact.title);
	printf("请输入手机：");
	
	scanf(" %s", new_Contact.phone);
	printf("请输入Email：\n");

	scanf(" %s", new_Contact.email);
	
	list->contacts[list->contact_Count++] = new_Contact;
	printf("联系人添加成功。\n"); 
} 

// 删除联系人函数
void delete_Contact(ContactList *list){
	char name_ToDelete[MAX_NAME];
	printf("请输入要删除的联系人姓名：");
	scanf("%s", name_ToDelete); 
	int found = 0;
	for(int i = 0; i < list->contact_Count; i++){
		if(strcmp(list->contacts[i].name, name_ToDelete) == 0){
			found = 1;
			for(int j = i; j < list->contact_Count - 1; j++){
				list->contacts[j] = list->contacts[j + 1];
			}
			list->contact_Count--;
			printf("联系人删除成功。\n");
			break;
		}
	}
	if (!found){
		printf("未找到该联系人。\n");
	}
}

// 修改联系人函数
void modify_Contact(ContactList *list){
	char name_ToModify[MAX_NAME];
	printf("请输入要修改的联系人姓名：");
	scanf("%s", name_ToModify);
	int found = 0;
	for (int i = 0; i < list->contact_Count; i++){
		if(strcmp(list->contacts[i].name, name_ToModify) == 0){
			found = 1;
			Contact *contact = &list->contacts[i];
			printf("请输入新的姓名：");
			scanf("%s", contact->name);
			if(strlen(contact->name) == 0){
				strcpy(contact->name, list->contacts[i].name);
			}
			printf("请输入新的职务：");
			scanf("%s", contact->job);
			if(strlen(contact->job) == 0){
				strcpy(contact->job, list->contacts[i].job);
			}
			printf("请输入新的职称：");
			scanf("%s", contact->title);
			if(strlen(contact->title) == 0){
				strcpy(contact->title, list->contacts[i].title);
			}
			printf("请输入新的手机：");
			scanf("%s", contact->phone);
			if(strlen(contact->phone) == 0){
				strcpy(contact->phone, list->contacts[i].phone);
			}
			printf("请输入新的Email：");
			scanf("%s", contact->email);
			if(strlen(contact->email) == 0){
				strcpy(contact->email, list->contacts[i].email);
			}
			printf("联系人修改成功。\n");
			break; 
		}
	}
	if(!found){
		printf("未找到该联系人。\n");
	}
}

// 显示联系人函数
void display_Contacts(ContactList *list){
	if(list->contact_Count == 0){
		printf("通讯录为空。\n");
		return;
	}
	for(int i = 0; i < list->contact_Count; i++){
		printf("姓名：%s\n", list->contacts[i].name);
		printf("职务：%s\n", list->contacts[i].job);
		printf("职称：%s\n", list->contacts[i].title);
		printf("手机：%s\n", list->contacts[i].phone);
		printf("Email: %s\n", list->contacts[i].email);
		printf("-----------------\n");		
	}
}


void search_ContactsByName(ContactList *list){
	char name_ToSearch[MAX_NAME];
	printf("请输入要查找的联系人姓名：");
	scanf("%s", name_ToSearch);
	int found = 0;
	for(int i = 0; i < list->contact_Count; i++){
		if(strstr(list->contacts[i].name, name_ToSearch) != NULL){
			found = 1;
			printf("姓名：%s\n", list->contacts[i].name);
			printf("职务：%s\n", list->contacts[i].job);
			printf("职称：%s\n", list->contacts[i].title);
			printf("手机：%s\n", list->contacts[i].phone);
			printf("Email：%s\n",list->contacts[i].email);
			printf("-----------------\n");
		}
	}
	if(!found){
		printf("未找到相关联系人。\n");
	}
}

// 按其他属性查找联系人函数（示例按手机查找）
void search_ContactsByPhone(ContactList *list){
	char phone_ToSearch[MAX_PHONE];
	printf("请输入要查找的手机号码：");
	scanf("%s",phone_ToSearch);
	int found = 0;
	for(int i = 0; i < list->contact_Count; i++){
		if(strstr(list->contacts[i].phone, phone_ToSearch) != NULL){
			found = 1;
			printf("姓名：%s\n", list->contacts[i].name);
			printf("职务：%s\n", list->contacts[i].job);
			printf("职称：%s\n", list->contacts[i].title);
			printf("手机：%s\n", list->contacts[i].phone);
			printf("Email：%s\n", list->contacts[i].email);
			printf("-----------------\n"); 
		}
	}
	if(!found){
		printf("未找到相关联系人。\n");
	}
}

// 按姓名排序联系人函数
void sort_ContactsByName(ContactList *list){
	for(int i = 0; i < list->contact_Count - 1; i++){
		for(int j = i + 1; j < list->contact_Count; j++){
			if(strcmp(list->contacts[i].name, list->contacts[j].name) > 0){
				Contact temp = list->contacts[i];
				list->contacts[i] = list->contacts[j];
				list->contacts[j] = temp;
			}
		}
	}
	printf("联系人已按姓名排序。排序结果如下：\n");
	display_Contacts(list); 
}

int main(){
	ContactList list = { .contact_Count = 0};
	int choice;
	system("cls");
	while(1){
		printf("\n欢迎使用通讯录管理系统\n");
		printf("1. 添加联系人\n");
		printf("2. 删除联系人\n");
		printf("3. 修改联系人\n");
		printf("4. 显示所有联系人\n");
		printf("5. 按姓名查找联系人\n");
		printf("6. 按其他属性查找联系人\n");
		printf("7. 按姓名排序联系人\n");
		printf("8. 退出\n");
		printf("请选择操作：");
		scanf("%d", &choice);
		
		switch(choice){
			case 1:
				system("cls");
				addContact(&list);
				system("pause");
				break;
			case 2:
				system("cls");
			    delete_Contact(&list);
			    system("pause");
				break;
			case 3:
				system("cls");
			    modify_Contact(&list);
			    system("pause");
				break;
			case 4:
				system("cls");
			    display_Contacts(&list);
			    system("pause");
			    break;
			case 5:
				system("cls");
			    search_ContactsByName(&list);
			    system("pause");
				break;
			case 6:
				system("cls");
			    search_ContactsByPhone(&list);
			    system("pause");
				break;
			case 7:
				system("cls");
			    sort_ContactsByName(&list);
			    system("pause");
				break;
			case 8:
				system("cls");
			    exit(0);
			    system("pause");
			default:
			    printf("无效选择，请重新输入。\n");								
		} 
	}
	return 0;
} 